<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/20
 * Time: 11:14
 */

namespace app\index\controller;
use think\db;
use think\facade\Session;

class Traincar extends Base
{
    public function token()
    {
        $token = $this->request->token('__token__', 'sha1');
        return json(['code'=>1,'__token__'=>$token]);
    }
    //预约练车
    public function select(){
        return $this->fetch();
    }
    public function selectData(){
        //科目
        $class=Db::name('class_type')
            ->select();
        foreach ($class as $k=>$v){
            if(strstr($v['class_type'],'3')==false){
                unset($class[$k]);
            }
        }
        $class=array_values($class);
        //驾照
        $driver=Db::name('Driver_type')
            ->select();
        return json(['code'=>1,'msg'=>'success','class'=>$class,'driver'=>$driver]);
    }
    //周末预约练车选择
    public function traincarSure(){
        //获取教练信息
        $coach=Db::name('coach')
            ->where('is_del',0)
            ->select();
        $this->assign('coach',$coach);
        return $this->fetch();
    }
    //获取周天预约的时间
    public function traincarday(){
        $get=input('get.');
        $data['jzid']=$get['jzid'];
        $data['kmid']=$get['kmid'];
        $data['is_del']=0;
        $data['typeid']=1;
        //根据驾照和科目查询可预约的数据
        $yuyue=Db::name('yuyue')
            ->where($data)
            ->find();
        $yuyuetype=Db::name('yuyue_type')
            ->where('type',1)
            ->find();
        if($yuyue){
            $date=explode('&',$yuyue['senvenday']);
            return json(['code'=>1,'msg'=>'success','data'=>$date,'yuyue'=>$yuyue,'yuyuetype'=>$yuyuetype]);
        }
        return json(['code'=>0,'msg'=>'没有数据']);
    }
    public function traincarsuredata(){
        $get=input('get.');
        $data['jzid']=$get['jzid'];
        $data['kmid']=$get['kmid'];
        $data['is_del']=0;
        $data['typeid']=1;
        //根据驾照和科目查询可预约的数据
        $yuyue=Db::name('yuyue')
            ->where($data)
            ->find();
        //获取教练信息
        $coach=Db::name('coach')
            ->where('is_del',0)
            ->select();
        return json(['code'=>1,'yuyue'=>$yuyue,'coach'=>$coach]);
    }
    public function ordersubmit7(){
        $post=input('post.');
        $validate = $this->validate($post,'app\index\validate\Traincar');
        if (true !== $validate){
            if($validate=='你已经提交过了'){
                $token = $this->request->token('__token__', 'sha1');
                return json(['code'=>1000,'msg'=>'你已提交过了!','__token__'=>$token]);
            }
            return json(['code'=>0,'msg'=>$validate]);
        }
        $userid=Session::get('user_id');
        $wheress['user_id']=$userid;
        $wheress['order_status']=1;
        $wheress['test_type']=1;
        $userorder=Db::name('order_list')
            ->where($wheress)
            ->find();
        if(!$userorder){
            return json(['code'=>-3,'msg'=>'您还没有参加报名']);
        }
        $user=Db::name('member')
            ->where('id',$userid)
            ->find();
        if(!$user){
            return json(['code'=>-3,'msg'=>'没有此用户']);
        }
        $get=input('post.');
        $yyid=$get['id'];
        
        $coachid=$get['coachid'];
        $yuyue=Db::name('yuyue')
            ->where('id',$yyid)
            ->find();
        $jzid=$yuyue['jzid'];
        $kmid=$yuyue['kmid'];
        $begin=$get['begin'];
        $end=$get['end'];
        $hbegin=(int)date('H',strtotime($begin));
        $hend=(int)date('H',strtotime($end));
        if($hbegin<=11&&$hend>=14){
            $hour=($hend-$hbegin)-3;
        }else{
            $hour=$hend-$hbegin;
        }
        if($user){
            $data['true_name']=$user['true_name'];
            $data['phone']=$user['phone'];
        }
        $data['order_no']=$this->randomStr().time();
        $data['good_name']='预约练车周天';
        $data['user_id']=$userid;
        $data['image']='/static/images/yuyue.png';
        $data['traincar_shichang']=$hour;
        $data['traincar_date']=$begin.'//'.$end;
        $data['test_type']=3;
        $data['coach_id']=$coachid;
        $data['traincar_yyid']=$yyid;
        $data['sign_type']=$jzid;
        $data['course_type']=$kmid;
        $data['create_time']=time();
        $data['pay_money']=$hour*$yuyue['price'];
        $id=Db::name('order_list')->insertGetId($data);
        if($id){
            return json(['code'=>1,'msg'=>'预约成功','url'=>'/index.php/index/wxpay/traincarPay?id='.$id]);
        }
        return json(['code'=>0,'msg'=>'预约失败']);
    }
    //1-6预约界面
    public function traincarSubmit(){
        $type=Db::name('yuyue_type')
            ->where('type',0)
            ->find();
        $this->assign('type',$type);
        return $this->fetch();
    }
    //1-6预约界面数据
    public function traincarSubmitData(){
        $get=input('post.');
        $data['jzid']=$get['jzid'];
        $data['kmid']=$get['kmid'];
        $data['is_del']=0;
        $data['typeid']=0;
        //根据驾照和科目查询可预约的数据
        $yuyue=Db::name('yuyue')
            ->where($data)
            ->find();
        if(!$yuyue){
            return json(['code'=>-1,'msg'=>'没有数据']);
        }
        //可预约的日期
        $spec=Db::name('yuyue_spec')
            ->where('yyid',$yuyue['id'])
            ->order('date','asc')
            ->select();
        $data=null;
        foreach ($spec as $k=>$v){
            $data1['dateid']=$v['id'];
            $data1['yyid']=$yuyue['id'];
            $detail=Db::name('yuyue_spec_detail')
                ->where($data1)
                ->order('id','asc')
                ->select();
            foreach ($detail as $kk=>$vv){
                $tempp=$this->getkucun($vv['id']);
                $detail[$kk]['date']=$v['date'];
                $weekarray=array("日","一","二","三","四","五","六");
                $detail[$kk]['week']="星期".$weekarray[date("w",strtotime($v['date']))];
                $detail[$kk]['shengcount']=$vv['count']-($tempp['freecount1']+$tempp['paycount1']);
                if($tempp['freecount']>$tempp['dangricount']){
                    $detail[$kk]['pay']=0;
                }else{
                    $detail[$kk]['pay']=$vv['price'];
                }
            }
            $data[$k]=$detail;
        }
        $coach=Db::name('coach')
            ->where('id','in',explode(',',$spec[0]['coachid']))
            ->select();
        return json(['code'=>1,'msg'=>'success','data'=>$data,'coach'=>$coach]);
    }
    //获取所选日期，库存
    public function gettongzhi(){
        $get=input('post.');
        //日期id
        $id=$get['id'];
        //当日预约人数统计
        $where3['traincar_dateid']=$id;
        $where3['order_status']=1;
        $where3['test_type']=3;
        $dangricount=Db::name('order_list')
            ->where($where3)
            ->count();
        //查询免费库存和收费库存
        $temp=Db::name('yuyue_type')
            ->where('type',0)
            ->find();
        $freecount=$temp['free_count'];
        $paycount=$temp['pay_count'];
        $yuyuespec=Db::name('yuyue_spec')
            ->where('id',$id)
            ->find();
        $arr['date']=$yuyuespec['date'];
        $arr['totalcount']=$freecount+$paycount;
        $arr['shengcount']=$arr['totalcount']-$dangricount;
        return json(['code'=>1,'msg'=>'success','data'=>$arr]);
    }
    //预约时选择日期获取当日教练
        public function getcoach(){
        $get=input('post.');
        //日期id
        $id=$get['id'];
        $yuyuespec=Db::name('yuyue_spec')
            ->where('id',$id)
            ->find();
        //查询免费库存和收费库存
        $temp=Db::name('yuyue_type')
            ->where('type',0)
            ->find();
        $freecount=$temp['free_count'];
        $paycount=$temp['pay_count'];
        //当日预约人数统计
        $where3['traincar_dateid']=$id;
        $where3['order_status']=1;
        $where3['test_type']=3;
        $dangricount=Db::name('order_list')
            ->where($where3)
            ->count();
        $arr['date']=$yuyuespec['date'];
        $arr['totalcount']=$freecount+$paycount;
        $arr['shengcount']=$arr['totalcount']-$dangricount;
        //判断当日库存
        if(($freecount+$paycount)<=$dangricount){
            return json(['code'=>-1,'msg'=>'库存为零']);
        }
        //获取当天的教练id集合
        $coachid=Db::name('yuyue_spec')
            ->field('coachid')
            ->where('id',$id)
            ->find();
        $coach=Db::name('coach')
            ->where('id','in',explode(',',$coachid['coachid']))
            ->select();
        foreach ($coach as $k=>$v){
            if($freecount>$dangricount){
                $where['traincar_dateid']=$id;
                $where['coach_id']=$v['id'];
                $where['pay_money']=0;
                $where['order_status']=1;
                $where['test_type']=3;
                $countss=Db::name('order_list')
                    ->where($where)
                    ->count();
                if($countss==($freecount/$temp['coachcount'])){
                    unset($coach[$k]);
                }
            }else{
                $where['traincar_dateid']=$id;
                $where['coach_id']=$v['id'];
                $where['order_status']=1;
                $where['test_type']=3;
                $countss=Db::name('order_list')
                    ->where($where)
                    ->where('pay_money','gt',0)
                    ->count();
                if($countss==($paycount/$temp['coachcount'])){
                    unset($coach[$k]);
                }
            }

        }
        if(!$coachid){
            return json(['code'=>-1,'msg'=>'当天没有教练']);
        }
        $coach=array_values($coach);
        return json(['code'=>1,'msg'=>'success','data'=>$coach,'temp'=>$arr]);
    }
    //1-6添加数据到订单中
    public function ordersubmit(){
        $post=input('post.');
        $userid=Session::get('user_id');
        $validate = $this->validate($post,'app\index\validate\Traincar');
        if (true !== $validate){
            if($validate=='你已经提交过了'){
                $token = $this->request->token('__token__', 'sha1');
                return json(['code'=>1000,'msg'=>'你已提交过了!','__token__'=>$token]);
            }
            return json(['code'=>0,'msg'=>$validate]);
        }
        $wheress['user_id']=$userid;
        $wheress['order_status']=1;
        $wheress['test_type']=1;
        $flag=Db::name('order_list')
            ->where($wheress)
            ->find();
        if(!$flag){
            return json(['code'=>-3,'msg'=>'您还没有参加报名']);
        }
        $user=Db::name('member')
            ->where('id',$userid)
            ->find();
        if(!$user){
            return json(['code'=>-3,'msg'=>'没有此用户']);
        }
        $get=input('post.');
        $coachid=$get['coachid'];
        $timeid=$get['timeid'];
        //获取所选时间段的数据
        $specdetail=Db::name('yuyue_spec_detail')
            ->where('id',$timeid)
            ->find();
        $yyid=$specdetail['yyid'];
        $dateid=$specdetail['dateid'];
        //此用户当日是否预约
        $flag=Db::name('order_list')
            ->where('traincar_dateid',$dateid)
            ->where('user_id',$userid)
            ->where('order_status',1)
            ->find();
        if($flag){
            return json(['code'=>-2,'msg'=>'此时间段已经预约，请勿重复预约']);
        }
        //查询免费库存和收费库存
        $temp=Db::name('yuyue_type')
            ->where('type',0)
            ->find();
        $freecount=$temp['free_count'];
        $paycount=$temp['pay_count'];
        //当日预约人数统计
        $where3['traincar_dateid']=$dateid;
        $where3['order_status']=1;
        $where3['test_type']=3;
        $dangricount=Db::name('order_list')
            ->where($where3)
            ->count();
        //判断当日库存
        if(($freecount+$paycount)<=$dangricount){
            return json(['code'=>-1,'msg'=>'库存为零']);
        }

        //获取预约数据
        $yuyue=Db::name('yuyue')
            ->where('id',$yyid)
            ->find();
        //获取所选日期
        $where['yyid']=$yyid;
        $where['id']=$dateid;
        $spec=Db::name('yuyue_spec')
            ->field('date')
            ->where($where)
            ->find();
        //获取库存
        $tempp=$this->getkucun($timeid);
        //如果当日所选时间段库存为零
        if(($specdetail['count']-($tempp['freecount1']+$tempp['paycount1']))==0){
            return json(['code'=>-3,'msg'=>'此时间段库存为零']);
        }
        //如果当日免费库存大于预约人数
        if($tempp['freecount']>$tempp['dangricount']){
            $data['pay_money']=0;
            $data['pay_time']=time();
            $data['order_status']=1;
        }else{
            $data['pay_money']=$specdetail['price'];
            $data['order_status']=0;
        }
        if($user){
            $data['true_name']=$user['true_name'];
            $data['phone']=$user['phone'];
        }
        $data['order_no']=$this->randomStr().time();
        $data['user_id']=$userid;
        $data['image']='/static/images/yuyue.png';
        $data['good_name']='预约练车周一到周六';
        $data['traincar_yyid']=$yyid;
        $data['traincar_timeid']=$timeid;
        $data['traincar_dateid']=$dateid;
        $data['test_type']=3;
        $data['sign_type']=$yuyue['jzid'];
        $data['course_type']=$yuyue['kmid'];
        $data['coach_id']=$coachid;
        $data['create_time']=time();
        $data['traincar_date']=$spec['date'].'-'.$specdetail['time'];
        $data['traincar_dateid']=$dateid;
        $data['traincar_timeid']=$timeid;
        $flag=Db::name('order_list')->insert($data);
        $orderid=Db::name('order_list')->getLastInsID();
        if($flag){
            if($data['pay_money']==0){
                return json(['code'=>1,'url'=>'/index/order/orderList']);
            }
            return json(['code'=>2,'url'=>'/wxpay/traincarPay?id='.$orderid]);
        }
        return json(['code'=>-4,'msg'=>'error']);
    }
    //获取当日某时间段免费预约统计和收费预约统计
    public function getkucun($timeid){
        //查询免费库存和收费库存
        $temp=Db::name('yuyue_type')
            ->where('type',0)
            ->find();
        $freecount=$temp['free_count'];
        $paycount=$temp['pay_count'];
        //当日预约人数统计
        $dateid=Db::name('yuyue_spec_detail')
            ->field('dateid')
            ->where('id',$timeid)
            ->find();
        $where3['traincar_dateid']=$dateid['dateid'];
        $where3['order_status']=1;
        $where3['test_type']=3;
        $dangricount=Db::name('order_list')
            ->where($where3)
            ->count();
        //此时间段免费人数的统计
        $where1['traincar_timeid']=$timeid;
        $where1['pay_money']=0;
        $where1['order_status']=1;
        $where1['test_type']=3;
        $freecount1=Db::name('order_list')
            ->where($where1)
            ->count();
        //此时间段收费人数的统计
        $where2['traincar_timeid']=$timeid;
        $where2['order_status']=1;
        $where2['test_type']=3;
        $paycount1=Db::name('order_list')
            ->where($where2)
            ->where('pay_money','gt',0)
            ->count();
        return array('freecount'=>$freecount,'paycount'=>$paycount,'dangricount'=>$dangricount,'freecount1'=>$freecount1,'paycount1'=>$paycount1);
    }
    //随机数
    public function randomStr(){
        $arr = array_merge(range(0,9),range('A','Z'));
        $str = '';
        $arr_len = count($arr);
        for($i = 0;$i < 8;$i++){
            $rand = mt_rand(0,$arr_len-1);
            $str.=$arr[$rand];
        }
        return $str;
    }
}